package com.example.server.controller;

import com.example.server.pojo.Admin;
import com.example.server.security.Authenticated;
import com.example.server.security.PreAuthorize;
import com.example.server.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController // @ResponseBody + @Controller
@Api(tags = "测试控制器")
public class TestController {

    @GetMapping("/test")
    @ApiOperation("测试1")
    @Authenticated
    @PreAuthorize(roles = {"ROLE_test", "ROLE_admin"})
    public String test() {
        return "success!";
    }

    /**
     * @RequestParam
     *  HttpServletRequest#getParameter()
     *  请求参数 /xxx/ccc?id=xx
     *  form表单
     * @param admin
     * @return
     */
    @GetMapping("/test2")
    @ApiOperation("测试2")
    public RespBean test2(Admin admin) {

        return RespBean.success();
    }

    /**
     *
     * @param admin
     * @return
     */
    @PostMapping("/test3")
    @ApiOperation("测试3")
    public RespBean test3(@RequestBody Admin admin) {

        return RespBean.success(200, "请求成功", admin);
    }

    /**
     * /test4/x
     * @return
     */
    @GetMapping("/test4/{id}")
    @ApiOperation("测试4")
    public RespBean test4(@PathVariable("id") String id, @RequestHeader String token) {

        return RespBean.success(id + "\n" + token);

    }
}
